package com.szholly.data.jdbc.util;

import java.io.Reader;

import oracle.sql.BLOB;
import oracle.sql.CLOB;

import org.apache.log4j.Logger;

@SuppressWarnings("deprecation")
public class OracelClobStringUtil {

	private static Logger logger = Logger.getLogger(OracelClobStringUtil.class);

	public static String Clob2String(CLOB clob) {
		String content = null;
		StringBuffer stringBuf = new StringBuffer();
		try {
			int length = 0;
			Reader inStream = clob.getCharacterStream(); 
			char[] buffer = new char[10];
			while ((length = inStream.read(buffer)) != -1) 
			{
				for (int i = 0; i < length; i++) {
					stringBuf.append(buffer[i]);
				}
			}

			inStream.close();
			content = stringBuf.toString();
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.info(ex.getMessage());
		}
		return content;
	}

	public static byte[] Blob2Byte(BLOB blob) {
		byte[] msgContent = blob.getBytes(); 
		return msgContent;
	}

	public static String Blob2String(BLOB blob) {
		String newStr = ""; // �����ַ�
		long BlobLength; // BLOB�ֶγ���
		byte[] bytes; // BLOB��ʱ�洢�ֽ�����
		int i = 1; // ѭ������
		int depth = 100000;

		try {
			byte[] msgContent = blob.getBytes(); // BLOBת��Ϊ�ֽ�����
			BlobLength = blob.length(); // ��ȡBLOB����?
			if (msgContent == null || BlobLength == 0) // ���Ϊ�գ����ؿ�ֵ?
			{
				return "";
			} else {
				while (i < BlobLength) // ѭ�������ַ�ת����ÿ��1024��Oracle�ַ��������4k
				{
					bytes = blob.getBytes(i, depth);
					i = i + depth;
					newStr = newStr + new String(bytes, "gb2312");
				}

			}
		} catch (Exception ex) {
			ex.printStackTrace();
			logger.info(ex.getMessage());
		}
		return newStr;
	}

}
